#!/usr/bin/python
import pom_joe, random
from pom_joe import *

class POM_Interface(object):
    
    def __init__(self):
        self.mclass = []
        self.priority = []
        self.learner = []
    
    def run_POM(self, late=1.00, params = [randpay(), randpay(), randpay(), randpay(), randpay()]):
        score,input =  pom_demo(late, params)
        return score,input
    
    def test_POM(self, iters = 25, noprint = False):
        #Run POM a number of times and view the averages and variances
        scores = []
        inputs = []
        
        m = [randpay(), randpay(), randpay(), randpay(), randpay()]
        for i in range(iters):
            
            for i in range(5):
                m[i] = randpay()
        
            for i in range(len(self.mclass)):
                m[self.mclass[i]-1] = undiscretize((self.priority[i]))
                
            score,input = self.run_POM(1, m)
            scores.append(score)
            inputs.append(input)
        averages = [matrix_avg(scores)]
        variances = [matrix_var(scores)]
        
        #compile list data format
        
        pom_data = []
        for i,v in enumerate(inputs):
            pom_data.append([inputs[i], scores[i]])
        
        for i in pom_data: print i
            
            
        if not noprint: self.pom_report(scores, averages, variances)
            
        return scores, averages, variances, pom_data
        


    #Method to pretty print a report on scores, averages, and variances
    def pom_report(self, scores, averages, variances):
        print "Scores --- "  
        pretty_print(scores)
        print "Averages --- "
        pretty_print(averages)
        print "Variances --- "
        pretty_print(variances)
        
    def test_learner(self, pom_iters = 25, learner_iters = 20):
       self.mclass = []
       self.learner = []
       self.priority = []
       m = [randpay(), randpay(), randpay(), randpay(), randpay()]
       #m = [randpri(), randpri(), randpri(), randpri(), randpri(), randpri(), randpri()] 
       averages = []
       variances = []
       noprint = True
           
       for j in range(learner_iters):
           
           print "Learning Iteration #" + str(j)
          
           #run pom and collect data, statistics
           _,avg,var,pom_data = self.test_POM(pom_iters, noprint)
           averages.append(avg)
           variances.append(var)
           
           #compile list data and send it to the learner
           readData, readScore = (joeread.readJoe(pom_data))
           res = bore.bored3(readData)
           
           #stop if learner has nothing to say
           if (len(res) < 1): break
           
           #update routine
           for i in range(1):
                self.mclass.append((int)(str(res[i]).split(',')[1][4]))
                s = (str(res[i]).split(',')[2].strip(")").strip(" "))
                self.priority.append(((int)(s)))
                self.learner.append((float)(str(res[i]).split(",")[0].strip("(").strip(" ")))
                
       for i in averages: pretty_print(i)

#LOAD UP SCRIPT
seed = random.randint(1,100000)
for i,v in enumerate(sys.argv):
    if v == "-p":
        print "\n Output Suppressed\n"
        PRINT_FLAG = False
    if v == "-s":
        seed = sys.argv[i+1]
random.seed(seed)

POM_CONTROLLER = POM_Interface()
POM_CONTROLLER.test_POM()